/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.jsonrpc;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.b3log.latke.action.ActionException;
import org.b3log.latke.jsonrpc.AbstractJSONRpcService;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.Locales;
import org.b3log.nide.model.source.java.JavaSourceFile;
import org.b3log.nide.service.NideJavaCompiler;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Compiles file service for JavaScript client.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.6, Nov 29, 2010
 */
public final class CompileService extends AbstractJSONRpcService {

    /**
     * Default serial version uid.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Logger.
     */
    private static final Logger LOGGER = 
            Logger.getLogger(CompileService.class.getName());
    /**
     * Language service.
     */
    private LangPropsService languageService;
    /**
     * Nide Java compiler.
     */
    private NideJavaCompiler nideJavaCompiler;

    /**
     * Diagnoses a single Java source file by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example:
     * <pre>
     * {
     *     "fileName": "HelloWorld.java",
     *     "javaSourceCode": "...."
     * }
     * </pre>
     * @param request the specified http servlet request
     * @return as the same as return
     *          {@linkplain NideJavaCompiler#compile(java.lang.String, java.lang.String, java.util.Locale) }
     * @throws ActionException action exception
     */
    public JSONObject diagnose(final JSONObject requestJSONObject,
                               final HttpServletRequest request)
            throws ActionException {
        final Locale locale = Locales.getLocale(request);
        String sourceCode = null;
        String fileName = null;

        try {
            sourceCode = requestJSONObject.getString(
                    JavaSourceFile.JAVA_SOURCE_CODE);
            fileName = requestJSONObject.getString(JavaSourceFile.FILE_NAME);
        } catch (final JSONException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new ActionException(e);
        }

        JSONObject ret = null;
        try {
            ret = nideJavaCompiler.compile(fileName, sourceCode, locale);
        } catch (final ServiceException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new ActionException(e);
        }

        return ret;
    }
}
